<template>
  <div class="mod-config mod-menu">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input size="mini" v-model="dataForm.key" placeholder="参数名" clearable @clear="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList()">查询</el-button>
        <el-button size="mini" v-if="isAuth('goods:goodscategory:save')" type="primary" @click="addOrUpdateHandle()">新增
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区 -->
    <tree-table size="mini" ref="treeTable" :data="dataList" :columns="columns" :selection-type="false" :expand-type="false"
      :show-index="true" index-text="序号" border  :show-row-hover="true">
      <template slot="catLevel" slot-scope="scope">
        <el-tag v-if="scope.row.catLevel === 1">一级</el-tag>
        <el-tag v-else-if="scope.row.catLevel === 2" type="success">二级</el-tag>
        <el-tag v-else="scope.row.catLevel === 3" type="danger">三级</el-tag>
      </template>
      <template slot="status" slot-scope="scope">
       <!-- <i class="el-icon-success lightgreen font16" v-if="scope.row.status === 1"></i>
        <i class="el-icon-erro lightred" v-else></i> -->
        <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
        <el-tag type="warning" v-else>禁用</el-tag>
      </template>
      <template slot="opt" slot-scope="scope">
        <el-button size="mini" type="text" @click="addOrUpdateHandle(scope.row.categoryId)">编辑</el-button>
        <el-button size="mini" type="text" @click="deleteHandle(scope.row.categoryId)">删除</el-button>
      </template>
    </tree-table>

    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './goodscategory-add-or-update'
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        columns: [{
            label: '分类名称',
            prop: 'catName',
            width: '400px',
          },
          {
            label: '级别',
            prop: 'catLevel',
            minWidth: '50px',
            type: 'template',
            template: 'catLevel'
          },
          {
            label: '状态',
            prop: 'status',
            minWidth: '50px',
            type: 'template',
            template: 'status'
          },
          {
            label: '创建时间',
            prop: 'createTime',
            minWidth: '50px',
          },
          {
            label: '修改时间',
            prop: 'updateTime',
            minWidth: '50px',
          },
          {
            label: '操作',
            minWidth: '200px',
            type: 'template',
            template: 'opt'
          },
        ],
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/goods/goodscategory/listTree'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.categoryId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/goods/goodscategory/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>

<style scoped lang="less">
  .zk-table{
	border: none;
  }
	
  .zk-table__body-row {
      height: 35px;
  }
  .zk-table__header-row {
      background-color: #fdfdfe !important;
  }
</style>
